import React, {useEffect, useRef, useState} from 'react';
import {ActionType, ProColumns} from '@ant-design/pro-table';
import {modulesUrl} from "@/assets/constant/modulesUrl";
import TableList from "@/assets/components/tableList";
import {dateFormat, hooksSet} from "@/assets/utils/utils";
import {list} from "@/assets/service/baseService";
import {Button, Form, Switch} from "antd";
import {Link} from "react-router-dom";
import {UploadOutlined} from "@ant-design/icons";
import {Demand} from "@/pages/demand/DemandList/data";
import {useRequest} from "@@/plugin-request/request";
import {map} from "@/pages/sys/DictList/service";
import {dictType} from "@/assets/constant/dictType";

const DocList: React.FC<{}> = (props) => {
  // @ts-ignore
  const type = props.match.params.type;
  // 根据类型获取类型名称
  let typeName = "";
  switch (type) {
    case "1":
      typeName = "专题报告";
      break;
    case "2":
      typeName = "埋点文档";
      break;
    case "3":
      typeName = "故障报告";
      break;
  }

  const [form] = Form.useForm();
  const actionRef = useRef<ActionType>();

  /**
   * 获取字典列表
   * @type 字典类型
   */
  const typeDictMap = useRef([]);
  useRequest(() => map({type: dictType.DOC_TYPE}), {
    onSuccess: (result, params) => {
      typeDictMap.current = result;
      console.log(typeDictMap.current)
      console.log(typeDictMap.current)
    }
  })
  // @ts-ignore
  const [columns, setColumns] = useState<ProColumns<Demand>[]>([
    {
      title: (_, type, ___) => {
        return type === "form" ? "名称或描述" : "名称"
      },
      dataIndex: 'name',
      renderText: (text, record) => <Link to={"/doc/doc/" + type + "/"  + "form/" + record.id}>{text}</Link>,
    },
    {
      title: '发布人',
      dataIndex: 'createByUser',
      hideInSearch: true,
      renderText: (text, record) => record.createByUser?.name,
    },
    {
      title: '更新时间',
      dataIndex: 'updateDate',
      hideInSearch: true,
      renderText: (_, record) => dateFormat(record.updateDate),
    },
    {
      title: '浏览次数',
      dataIndex: 'browseNumber',
      hideInSearch: true,
    },
    {
      title: '是否置顶',
      dataIndex: 'isTop',
      hideInSearch: true,
      renderText: (text) => <Switch defaultChecked={text === "1"} disabled={true}/>
    },
  ]);

  /**
   * 启动加载
   */
  useEffect(() => {
    actionRef.current?.reload()
  }, [type]);

  return (
    <TableList
      title={typeName}
      actionRef={actionRef}
      form={form}
      isNotUseModalForm={true}
      changeColumns={(columns: any) => hooksSet(setColumns, columns)} columns={columns}
      moduleUrl={modulesUrl.docDoc}
      addOption={
        <Button type={"primary"}>
          <Link to={"/doc/doc/" + type + "/form/0"}><UploadOutlined/> 上传</Link>
        </Button>
      }
      formUrl={(id) => "/doc/doc/" + type + "/form/" + id}
      request={(params: any) => list(modulesUrl.docDoc, Object.assign({}, params, {"type": type}))}
    />
  )
}

export default DocList
